<template>
    <el-form :model="rider" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="骑手姓名" prop="name">
                    <el-input v-model="rider.name" placeholder="骑手姓名" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="初始密码" prop="pwd">
                    <el-input v-model="rider.pwd" placeholder="密码" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="rider.phone" placeholder="手机号" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="工作状态" prop="gzzt">
                    <el-select
                        v-model="rider.gzzt"
                        placeholder="请选择"
                        clearable
                    >
                        <el-option label="空闲" value="空闲"/>
                        <el-option label="忙碌" value="忙碌"/>
                        <el-option label="休息" value="休息"/>
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="工作收入" prop="gzsr">
                    <el-input v-model="rider.gzsr" placeholder="工作收入" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="评分" prop="mark">
                    <el-input v-model="rider.mark" placeholder="评分" clearable/>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>

    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits(["closeDialog", "flushPage"]);

const rider = ref({
    id: '',
    name: '',
    pwd: '123456',
    phone: '',
    gzzt: '空闲',
    gzsr: 0.0,
    mark: 5.0
})

// 取消：关闭 dialog
const doCancel = () => {
    emits("closeDialog");
}

// 新增
const doSave = async () => {
    console.log(rider.value);
    const {data} = await axios.post('/api/rider', rider.value);
    if (data > 0) {
        emits("flushPage");			// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>